package com.oauth.application.client;

import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.oauth.application.client.beans.ApplicationClientBean;
import com.oauth.application.client.rpc.ApplicationRemoteService;
import com.oauth.application.client.rpc.ApplicationRemoteServiceAsync;
import com.oauth.application.client.ui.BrowseWindow;
import com.oauth.application.client.ui.MyApplications;
import com.oauth.auth.client.beans.MemberClientBean;
import com.oauth.auth.client.rpc.MemberRemoteService;
import com.oauth.auth.client.rpc.MemberRemoteServiceAsync;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.components.client.ui.footer.FooterUI;
import com.oauth.components.client.ui.header.HeaderUI;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class LocalEntryPoint implements EntryPoint {

	private final MemberRemoteServiceAsync memberService = GWT.create(MemberRemoteService.class);

	private static LocalEntryPointUiBinder uiBinder = GWT.create(LocalEntryPointUiBinder.class);

	interface LocalEntryPointUiBinder extends UiBinder<DockLayoutPanel, LocalEntryPoint> {
	}

	@UiField
	HeaderUI header;

	@UiField
	FooterUI footer;

	@UiField
	FlowPanel centerPanel;

	@UiField
	MyApplications myApplications;

	@UiField
	BrowseWindow browseWindow;

	private final ApplicationRemoteServiceAsync applicationService = GWT
			.create(ApplicationRemoteService.class);

	public void onModuleLoad() {
		DockLayoutPanel p = uiBinder.createAndBindUi(this);

		memberService.getMember(new OAuthAsnyCallBack<MemberClientBean>() {
			public void onSuccess(MemberClientBean result) {
				header.setUserName(result.getEmail());
			}
		});

		applicationService.getMyApplications(new OAuthAsnyCallBack<List<ApplicationClientBean>>() {
			public void onSuccess(List<ApplicationClientBean> result) {
				myApplications.addApplications(result, applicationService);
			}
		});

		applicationService
				.getUnSubscribedApplications(new OAuthAsnyCallBack<List<ApplicationClientBean>>() {
					public void onSuccess(List<ApplicationClientBean> result) {
						browseWindow.initBrowseWidget(result, applicationService);
						header.setLoading(false);
					}
				});

		header.setLoading(false);
		RootLayoutPanel rp = RootLayoutPanel.get();
		rp.add(p);
		// RootPanel.get().add(dockPanel);
	}

}
